<?php
class Controller extends CController
{
	public $layout='//layouts/default';
	public $menu=array();
	public $breadcrumbs=array();
	
	protected function beforeAction($action)
	{
		$publicActions=array('login','signin');
        if (Yii::app()->user->isGuest && !in_array($action->id,$publicActions)) {
            Yii::app()->user->loginRequired();

        }
		// Disable jQuery UI
	  	Yii::app()->clientScript->scriptMap=array('jquery-ui.min.js'=>false);
		// Define IMAGES_URL
		$baseUrl = Yii::app()->getBaseUrl(true);
		$imageUrl = str_replace(basename($baseUrl), '', $baseUrl) . 'images/';
		define ('IMAGES_URL', $imageUrl);
		return parent::beforeAction($action);
	}
	
	public function getImageUrl()
	{
		$baseUrl = Yii::app()->getBaseUrl(true);
		return str_replace(basename($baseUrl), '', $baseUrl) . '/images/';
	}
	
	private $utfSign = array(
		'/a/', '/á/', '/à/', '/ả/', '/ạ/', '/ã/', 
		'/ă/', '/ắ/', '/ằ/', '/ẳ/',	'/ặ/', '/ẵ/', 
		'/â/', '/ấ/', '/ầ/', '/ẩ/', '/ậ/', '/ẫ/', 
		'/e/', '/é/', '/è/', '/ẻ/', '/ẹ/', '/ẽ/', 
		'/ê/', '/ế/', '/ề/', '/ể/', '/ệ/', '/ễ/', 
		'/i/', '/í/', '/ì/', '/ỉ/', '/ị/', '/ĩ/', 
		'/o/', '/ó/', '/ò/', '/ỏ/', '/ọ/', '/õ/', 
		'/ô/', '/ố/', '/ồ/', '/ổ/', '/ộ/', '/ỗ/', 
		'/ơ/', '/ớ/', '/ờ/', '/ở/', '/ợ/', '/ỡ/', 
		'/u/', '/ú/', '/ù/', '/ủ/', '/ụ/', '/ũ/', 
		'/ư/', '/ứ/', '/ừ/', '/ử/', '/ự/', '/ữ/', 
		'/y/', '/ý/', '/ỳ/', '/ỷ/', '/ỵ/', '/ỹ/', 
		'/đ/', 
		'/A/', '/À/', '/Á/', '/Ạ/', '/Ả/', '/Ã/', 
		'/Â/', '/Ầ/', '/Ấ/', '/Ậ/', '/Ẩ/', '/Ẫ/', 
		'/Ă/', '/Ằ/', '/Ắ/', '/Ặ/', '/Ẳ/', '/Ẵ/', 
		'/E/', '/È/', '/É/', '/Ẹ/', '/Ẻ/', '/Ẽ/', 
		'/Ê/', '/Ề/', '/Ế/', '/Ệ/', '/Ể/', '/Ễ/', 
		'/I/', '/Ì/', '/Í/', '/Ị/', '/Ỉ/', '/Ĩ/', 
		'/O/', '/Ò/', '/Ó/', '/Ọ/', '/Ỏ/', '/Õ/', 
		'/Ô/', '/Ồ/', '/Ố/', '/Ộ/', '/Ổ/', '/Ỗ/', 
		'/Ơ/', '/Ờ/', '/Ớ/', '/Ợ/', '/Ở/', '/Ỡ/', 
		'/U/', '/Ù/', '/Ú/', '/Ụ/', '/Ủ/', '/Ũ/', 
		'/Ư/', '/Ừ/', '/Ứ/', '/Ự/', '/Ử/', '/Ữ/', 
		'/Y/', '/Ỳ/', '/Ý/', '/Ỵ/', '/Ỷ/', '/Ỹ/', 
		'/Đ/',
		// Unknown character
		'/̉/', '/̀/', '/́/', '/̉/', '/̣/', '/̃/',
		'/"/', '/\'/', '/_/', '/“/','/”/'
	);

	private $utfNoneSign = array(
		'a', 'a', 'a', 'a', 'a', 'a',
		'a', 'a', 'a', 'a', 'a', 'a',
		'a', 'a', 'a', 'a', 'a', 'a',
		'e', 'e', 'e', 'e', 'e', 'e',
		'e', 'e', 'e', 'e', 'e', 'e',
		'i', 'i', 'i', 'i', 'i', 'i',
		'o', 'o', 'o', 'o', 'o', 'o',
		'o', 'o', 'o', 'o', 'o', 'o',
		'o', 'o', 'o', 'o', 'o', 'o',
		'u', 'u', 'u', 'u', 'u', 'u',
		'u', 'u', 'u', 'u', 'u', 'u',
		'y', 'y', 'y', 'y', 'y', 'y',
		'd',
		'A', 'A', 'A', 'A', 'A', 'A',
		'A', 'A', 'A', 'A', 'A', 'A',
		'A', 'A', 'A', 'A', 'A', 'A',
		'E', 'E', 'E', 'E', 'E', 'E',
		'E', 'E', 'E', 'E', 'E', 'E',
		'I', 'I', 'I', 'I', 'I', 'I',
		'O', 'O', 'O', 'O', 'O', 'O',
		'O', 'O', 'O', 'O', 'O', 'O',
		'O', 'O', 'O', 'O', 'O', 'O',
		'U', 'U', 'U', 'U', 'U', 'U',
		'U', 'U', 'U', 'U', 'U', 'U',
		'Y', 'Y', 'Y', 'Y', 'Y', 'Y',
		'D',
		// Unknown character
		'', '', '', '', '', '',
		'', '', '', '', ''
	);

	public function formatUrl($value)
	{
		$strOut = preg_replace("/([\s]+)/","-",strtolower(trim($value)));
		$strOut = preg_replace("/([\W]+)/","-",strtolower(trim($strOut )));
		$strOut = preg_replace("/--/","-",strtolower(trim($strOut )));
		return $strOut;
	}

	public function convertString($value, $opt=0)
	{
		$r_value ="";
		if (!empty($value))
		{
			$r_value = trim(preg_replace($this->utfSign, $this->utfNoneSign, $value));
		}
		if ($opt == 1) {
			$r_value = trim(preg_replace('/\\\|\'/', '', $r_value));
		}
		if ($opt == 2) {
			$r_value = trim(preg_replace('/"/', '&quot;', $r_value));
		}
		return $r_value;
	}
	
	public function createSlug($value)
	{
		return $this->formatUrl($this->convertString($value, 1));
	}
	
	public function resizeImage($imagePath, $imageName, $postedTime)
	{
		// Set file out
		$ext = pathinfo($imageName, PATHINFO_EXTENSION);
		$name = pathinfo($imageName, PATHINFO_FILENAME);
		$dir = date('Ymd', strtotime($postedTime));
		// Create new Object
		$thumb = new Imagick();
		// Set Options
		$thumb->readImage($imagePath . $imageName);
		$thumb->setCompressionQuality(90);
		
		// Get current Image size
		$size = $thumb->getImageGeometry(); 
		// Resize #1: Set the original images
		$dest = IMAGES_DIR . 'events/primary/' . $dir . '/';
		if (!file_exists($dest)) {
			mkdir($dest, 0777, true);
		}
		if ($size['width'] > 1024 || $size['height'] > 576) {
			$thumb->resizeImage(1024, 576, Imagick::FILTER_LANCZOS, 1, true);
			$thumb->writeImage($dest . $imageName);
		}
		else {
			$thumb->writeImage($dest . $imageName);
		}
		// Resize #2
		$dest = IMAGES_DIR . 'events/slides/' . $dir . '/';
		if (!file_exists($dest)) {
			mkdir($dest, 0777, true);
		}
		$thumb->resizeImage(520, 285, Imagick::FILTER_LANCZOS, 1);
		$thumb->writeImage($dest . $imageName);
		// Resize #3
		$dest = IMAGES_DIR . 'events/thumbs/' . $dir . '/';
		if (!file_exists($dest)) {
			mkdir($dest, 0777, true);
		}
		$thumb->resizeImage(160, 90, Imagick::FILTER_LANCZOS, 1);
		$thumb->writeImage($dest . $imageName);
		// Resize Clear
		$thumb->clear();
		$thumb->destroy();
	}
	
	public function resizeChartImage($imagePath, $imageName, $postedTime)
	{
		// Set file out
		$ext = pathinfo($imageName, PATHINFO_EXTENSION);
		$name = pathinfo($imageName, PATHINFO_FILENAME);
		$dir = date('Ymd', strtotime($postedTime));
		// Create new Object
		$thumb = new Imagick();
		// Set Options
		$thumb->readImage($imagePath . $imageName);
		$thumb->setCompressionQuality(90);
		
		// Get current Image size
		$size = $thumb->getImageGeometry(); 
		// Resize
		$dest = IMAGES_DIR . 'events/charts/' . $dir . '/';
		if (!file_exists($dest)) {
			mkdir($dest, 0777, true);
		}
		if ($size['width'] > 1024 || $size['height'] > 576) {
			$thumb->resizeImage(1024, 576, Imagick::FILTER_LANCZOS, 1, true);
			$thumb->writeImage($dest . $imageName);
		}
		else {
			$thumb->writeImage($dest . $imageName);
		}
		// Resize Clear
		$thumb->clear();
		$thumb->destroy();
	}

    public function resizeNewsImage($imagePath, $imageName, $postedTime)
	{
		// Set file out
		$ext = pathinfo($imageName, PATHINFO_EXTENSION);
		$name = pathinfo($imageName, PATHINFO_FILENAME);
		$dir = date('Ymd', strtotime($postedTime));
		// Create new Object
		$thumb = new Imagick();
		// Set Options
		$thumb->readImage($imagePath . $imageName);
		$thumb->setCompressionQuality(80);
		
		// Get current Image size
		$size = $thumb->getImageGeometry(); 
		// Resize #1: Set the original images
		$dest = IMAGES_DIR . 'news/' . $dir . '/';
		if (!file_exists($dest)) {
			mkdir($dest, 0777, true);
		}
		if ($size['width'] > 1024 || $size['height'] > 576) {
			$thumb->resizeImage(1024, 576, Imagick::FILTER_LANCZOS, 1, true);
			$thumb->writeImage($dest . $imageName);
		}
		else {
			$thumb->writeImage($dest . $imageName);
		}
		// Resize Clear
		$thumb->clear();
		$thumb->destroy();
	}

	/**
     * List of allowd default Actions for the user
     * @return type 
     */
    public function allowedActions()
    {
           return 'login,logout';
    }
}